var detailObj = {
    lTeamId: "",
    userid: "",
    init: function() {
        this.userid = $.cookie('userid');
        this.lTeamId = publicObj.getUrlData("lTeamId");
        this.getGoodsDetail();
        this.wxJoinNow();
    },
    getGoodsDetail: function() {
        var me = this;
        var url = magic_config.urlPrefix + 'GroupbuyService/getallgroupbuyinfo';
        if (me.userid == null) {
            sUserIdStr = "";
        } else {
            sUserIdStr = me.userid;
        }
        var data = {
            lTeamId: me.lTeamId,
            sUserId: sUserIdStr
        }
        $.ajax({
            url: url,
            type: 'get',
            data: data,
            //async: false,
            dataType: 'json',
            success: function(res) {
                var nCode = res.return.nCode;
                if (nCode === 0) {
                    console.log(res.response);
                    me.judeStatus(res.response);
                    me.NewGoodsDetail(res.response.cdoData.lGroupBuyId);
                    var groupList = res.response.cdoDataList;
                    var groulpCount = res.response.cdoData.nCountPerGroup;
                    var remainNum = groulpCount - groupList.length;
                    $(".s_num").html(remainNum);
                } else {
                    console.log("数据请求失败，后到前");
                }
            },
            error: function(res) {
                console.log("数据请求失败，前到后");
            }
        });
    },
    judeStatus: function(resObj) {
        /*1.拼团成功
            a.用户参与了团购
            b.用户未参与团购
          2.拼团失败
            a.拼团失败
          3.团进行中
            a.参与团，走支付流程
        */
        console.log(resObj);
        var joinFlage = resObj.isJoinTream;
        var gStatus = resObj.cdoTreamInfo.nStatus;
        console.log(joinFlage, gStatus);
        if (gStatus == 9) {
            $(".wx_joinnow").hide();
            $(".endactive").show();
        } else if (gStatus == 2) {
            $(".wx_joinnow").hide();
            if (joinFlage) {
                $(".downnow").show();
            } else {
                $(".downnow").show();
            }
        } else if (gStatus == 0 || gStatus == 1) {
            $(".wx_joinnow").hide();
            $(".share").show();
        }
        if (publicObj.getUrlData("from") == "index") {
            $(".iswxbox").hide();
            $(".wx_joinnow").show();
        }
    },
    NewGoodsDetail: function(groupBuyId) {
        var me = this;
        var url = magic_config.urlPrefix + 'GroupbuyService/getgroupbuyinfo';
        var data = {
            lGroupBuyId: groupBuyId
        }
        console.log(data);
        $.ajax({
            url: url,
            type: 'get',
            data: data,
            //async: false,
            dataType: 'json',
            success: function(res) {
                var nCode = res.return.nCode;
                if (nCode === 0) {
                    console.log(res.response);
                    me.splitDetail(res.response.cdoData);
                } else {
                    console.log("数据请求失败，后到前");
                }
            },
            error: function(res) {
                console.log("数据请求失败，前到后");
            }
        });
    },
    splitDetail: function(goodsDetail) {
        //商品名字
        //$(".goodsname").html('【'+goodsDetail.sCorpName+'】'+goodsDetail.sName);

        $(".goodsname").html('<span class="dislogo">拼团价</span>&nbsp&nbsp【' + goodsDetail.sCorpName + '】' + goodsDetail.sName);
        //商品价格
        $(".goodspric").html('<span>¥</span>' + goodsDetail.nGroupPrice);
        $(".oprice").html("¥" + goodsDetail.nMainPrice);
        $(".pnum").html('<span>' + goodsDetail.nJoinerCount + '</span>人参与');
        //进度条
        if (goodsDetail.nSaleStock >= goodsDetail.nTotalStock) {
            goodsDetail.nSaleStock = goodsDetail.nTotalStock;
        }
        if (((goodsDetail.nSaleStock / goodsDetail.nTotalStock) * 100) <= 1) {
            var barStr = "1%";
        } else {
            var barStr = parseInt((goodsDetail.nSaleStock / goodsDetail.nTotalStock) * 100) + "%";
        }
        if(goodsDetail.nSaleStock == 0){
              var barStr = "0%";
        }
        console.log(barStr);
        $(".bartxt").html('已抢' + barStr)
        $(".progress").width(barStr);
        //倒计时
        this.getActiveEndTime(goodsDetail.dEndTime);
        // //插入图片详情
        var imgDetailStr = '';
        for (var i = 0; i < goodsDetail.sPictures.length; i++) {
            imgDetailStr += ' <div class="imgbox"><img src="' + goodsDetail.sPictures[i] + '" alt=""></div>';
        }
        $(".imglist").html(imgDetailStr);
        //顶部轮播
        var bannerStr = '';
        for (var i = 0; i < goodsDetail.sPictures.length; i++) {
            bannerStr += '<div class="swiper-slide"><div class="img_box2"><img src="' + goodsDetail.sPictures[i] + '" ></div></div>';
        }
        $(".swiper-wrapper").html(bannerStr);
        this.swiper();

    },
    //轮播图
    swiper: function() {
        var swiper = new Swiper('.swiper-container', {
            autoplay: true,
            autoplay: {
                speed: 1000,
                disableOnInteraction: false,
            },
            loop: true,
            centeredSlides: true,
            pagination: {
                el: '.swiper-pagination',
            },
        });
    },
    //倒计时
    getActiveEndTime: function(endTimeStr) {
        console.log(endTimeStr);
        endTimeStr = endTimeStr.replace(/-/g, '/');
        var me = this;
        var interval = setInterval(function() {
            var str = '';
            var endTime = (new Date(endTimeStr)).getTime();
            var date = new Date();
            var nowTime = date.getTime();
            endTime = parseInt(endTime / 1000);
            countdownTime = endTime - parseInt(nowTime / 1000);
            var day;
            var hour;
            var min;
            var sec;
            var allhour;
            day = parseInt(countdownTime / (3600 * 24));
            hour = parseInt((countdownTime - day * (3600 * 24)) / 3600);
            allhour = parseInt(countdownTime / 3600);
            min = parseInt((countdownTime - day * (3600 * 24) - hour * 3600) / 60);
            sec = countdownTime - day * (3600 * 24) - hour * 3600 - min * 60;
            if (sec <= 0 && min <= 0 && hour <= 0) {
                sec = "00";
                min = "00";
                hour = "00";
                allhour = "00";
            } else {
                if (Number(sec) < 10) {
                    sec = "0" + sec;
                }
                if (Number(min) < 10) {
                    min = "0" + min;
                }
                if (Number(hour) < 10) {
                    hour = "0" + hour;
                }
                if (Number(allhour) < 10) {
                    allhour = "0" + allhour;
                }
            }
            var a = '<span class="c-size">' + allhour + '</span> : <span class="c-size">' + min + '</span> : <span class="c-size">' + sec + '</span>';
            $(".end").html('距结束' + a);

        }, 1000);
    },
    //微信环境下点击立即参团
    wxJoinNow: function() {
        var me = this;
        $(".wx_joinnow").bind("click", function() {

            if (me.userid == null) {
                $(".layerbox").show();
                $(".normallogin").show();
            }
            if (me.userid != null) {
                statusCheckObj.init();
            }

        })
    }
}
detailObj.init();